﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Web.Security;

namespace PNProject.Controllers
{
    public partial class NewsController : Controller
    {
        public ActionResult Index(string p)
        {
            var member = Membership.GetUser();
            if (member != null)
            {
                var userName = member.UserName;
                Utils.TrackingUser(null, userName, "NewsController", "Index", Request.Url.PathAndQuery.ToString());
            }

            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            int page = PNLibraries.PageVerify(p);
            var list = StaticData.newsRepo.GetSome(a => a.Lang == lang && a.CType == 0).ToList();
            var model = list.Skip((page - 1) * 8).Take(8).ToList();
            ViewData["news"] = model;
            var count = list.Count;
            ViewData["page-total"] = count;
            int mod = count % 8;
            int pc = mod == 0 ? count / 8 : (count / 8) + 1;
            ViewData["page-pc"] = pc;
            ViewData["page-active"] = page;
            ViewData["page-list"] = PNLibraries.GetShowedPage(count, page, 8, 5);
            return View();
        }

        public ActionResult Detail(string id)
        {
            var member = Membership.GetUser();
            if (member != null)
            {
                var userName = member.UserName;
                Utils.TrackingUser(null, userName, "NewsController", "Detail", Request.Url.PathAndQuery.ToString());
            }
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            var dbo = StaticData.newsRepo.GetOne(a => a.TLink == id && a.Lang == lang);
            if (dbo == null)
                return RedirectToAction("Error", "Home");
            ViewData["news"] = dbo;
            var list = StaticData.newsRepo.GetSome(a => a.Lang == lang && a.TLink != id && a.CType == 0).ToList();
            ViewData["relative"] = list;
            return View();
        }

    }
}
